package com.chaochis.wind.user.web;

import com.chaochis.wind.common_exceptions.has_delete_exception.DataHasDeleteException;
import com.chaochis.wind.common_exceptions.not_active_exception.DataNotActiveException;
import com.chaochis.wind.common_exceptions.not_exist_exception.DataNotExistException;
import com.chaochis.wind.common_exceptions.other_login_exceptions.password_exception.UserPasswordErrorException;
import com.chaochis.wind.config.api.ApiResult;
import com.chaochis.wind.params.AdminLoginAccount;
import com.chaochis.wind.params.ResponseAdminUser;
import com.chaochis.wind.user.service.WindAccountService;
import com.chaochis.wind.user.service.WindLoginService;
import com.chaochis.wind.user.service.WindUserService;
import com.chaochis.wind.user.vo.WindAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * wind系统登录账号管理类
 * @author 刘超/chaochis
 */
@RestController
@RequestMapping("/admin/account")
public class WindAccountController {

  @Autowired
  private WindAccountService windAccountService;

  @Autowired@Qualifier("windAdminLoginService")
  private WindLoginService windLoginService;

  @Autowired
  private WindUserService windUserService;

  /**
   * 登录接口
   * @param adminLoginAccount 要登陆的账号
   * @return 返回用户信息与token
   */
  @PostMapping("/login")
  public ApiResult<ResponseAdminUser> adminLogin(@RequestBody AdminLoginAccount adminLoginAccount) throws DataNotExistException, DataHasDeleteException, DataNotActiveException, UserPasswordErrorException {
    // 把请求参数转换成要查询的WindAccount对象
    var loginAccount = new WindAccount();
    loginAccount.setAccountName(adminLoginAccount.getLoginAccountName());
    loginAccount.setAccountType(adminLoginAccount.getLoginAccountType());
    loginAccount.setAccountPassword(adminLoginAccount.getLoginPassword());
    // 执行登录
    var token = this.windLoginService.login(loginAccount);
    var user = this.windUserService.getTempUserInfoByToken(token);
    var resAdminUser = new ResponseAdminUser(token, user);
    return ApiResult.success(resAdminUser);
  }

  /**
   * 登出接口
   * @param request 当前的http请求
   * @return 是否成功退出登录
   */
  @GetMapping("/logout")
  public ApiResult<Boolean> adminLogout(HttpServletRequest request) {
    return ApiResult.success(true);
  }

  @PostMapping("/getList")
  public ApiResult<List<WindAccount>> getWindAccountList(){
    return ApiResult.success(this.windAccountService.getWindAccountList(new WindAccount()));
  }
}
